import axios from "axios";
import {getJwtToken, setJwtToken} from "@/lib/utils/TokenUtil";
import {Message, MessageBox} from "element-ui";
class HttpRequest{

    constructor(baseURL) {
        this.baseURL = baseURL;
    }

    initConfig(){
        const config = {
            baseURL:this.baseURL,
            headers:{}
        }
        return config;
    }
    //创建并返回axios实例，options参数为创建实例时传递的个性化参数baseURL
    request(options){
        const instance = axios.create();
        //设置请求拦截器
        instance.interceptors.request.use(
            config =>{
                if (config.url!=='/auth/login'){
                    config.headers['AccessToken'] = getJwtToken()
                }
                return config;
            })
        //在这里为axios实例instance设置响应拦截器
        instance.interceptors.response.use(
            response => {
                const res = response.data

                // if the custom code is not 20000, it is judged as an error.
                if (res.code !== 2000) {
                    Message({
                        message: res.message || 'Error',
                        type: 'error',
                        duration: 5 * 1000
                    })
                    // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
                    if (res.code === 5000 || res.code === 5012 || res.code === 5014) {
                        // to re-login
                        MessageBox.confirm('你已经被登出,你可以继续停留在当前页面,或者重新登录', 'Confirm logout', {
                            confirmButtonText: '重新登录',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            setJwtToken('')
                            location.reload()
                            location.href = '/'
                        }).catch((err)=>{
                            console.log(err)
                        })
                    }
                    return Promise.reject(new Error(res.message || 'Error'))
                } else {
                    return res
                }
            },
            error => {
                console.log('err' + error) // for debug
                if (error && error.response) {
                    switch (error.response.status) {
                        case 400:
                            error.message = '错误请求';
                            break;
                        case 401:
                            error.message = '未授权，请重新登录';
                            break;
                        case 403:
                            error.message = '拒绝访问';
                            break;
                        case 404:
                            error.message = '请求错误,未找到该资源';
                            break;
                        case 405:
                            error.message = '请求方法未允许';
                            break;
                        case 408:
                            error.message = '请求超时';
                            break;
                        case 500:
                            error.message = '服务器端出错';
                            break;
                        case 501:
                            error.message = '网络未实现';
                            break;
                        case 502:
                            error.message = '网络错误';
                            break;
                        case 503:
                            error.message = '服务不可用';
                            break;
                        case 504:
                            error.message = '网络超时';
                            break;
                        case 505:
                            error.message = 'http版本不支持该请求';
                            break;
                        default:
                            error.message = `未知错误${error.response.status}`;
                    }
                } else {
                    error.message = "连接到服务器失败";
                }
                Message({
                    message: error.message,
                    type: 'error',
                    duration: 5 * 1000
                })
                return Promise.reject(error)
            }
        );
        //将默认配置与个性化配置整合。
        //如果二者发生冲突，个性化配置options，覆盖初始化默认配置initConfig()
        options = Object.assign(this.initConfig(),options)

        return instance(options)
    }
}
export default HttpRequest
